Ext.setup( {
	icon : 'icon.png',
	tabletStartupScreen : 'tablet_startup.png',
	phoneStartupScreen : 'phone_startup.png',
	glossOnIcon : false,
	onReady : function() {
		var form;
		
		Ext.regModel('news', {
	   	    fields: [
	   	        {name: 'AGENCYID', type: 'string'},
	   	        {name: 'COMMITER', type: 'string'},
		        {name: 'DEPARTMENT', type: 'string'},
		        {name: 'TITLE', type: 'string'},
		        {name: 'NEWSTYPE', type: 'string'},
		        {name: 'URGENTTYPE', type: 'string'},
		        {name: 'CONTENTS', type: 'string'}
	   	    ],
	   	 validations: [ 
	   	               	  {type: 'presence', name: 'AGENCYID'},
			              {type: 'presence', name: 'TITLE'},
			              {type: 'presence', name: 'CONTENTS'}
			          ]
	   	});
		
		  Ext.Ajax.request({
	             url: '/wipzIphone/dic/dic_userAndDepariment.action',
	             success: function(response, opts) {
	        	     var jsonObj = eval("(" + response.responseText + ")");

	        	    	var user = Ext.ModelMgr.create({
	        	    		COMMITER  : jsonObj.COMMITER,
	        	    		DEPARTMENT : jsonObj.DEPARTMENT
	        	    	}, 'news');
	        	    
	        	    	form.load(user);
	             }
	         });
		
		 Ext.regModel('agenymapmodel', {
       	    fields: [
       	        {name: 'AGENCYID', type: 'string'},
       	        {name: 'NAME', type: 'string'}
       	    ]
       	});
  	
  	var agenymapStore = new Ext.data.Store({
  		autoLoad: true,
  	    model: 'agenymapmodel',
  	    proxy: {
  	        type: 'ajax',
  	        url : '/wipzIphone/dic/dic_agencyMap.action',
  	        reader: {
  	            type: 'json'
  	        }
  	    }
  	});
		
		var formBase = {
			scroll : 'vertical',
			standardSubmit : false,
			items : [ {
				xtype : 'fieldset',
				title : '所属单位',
				items : [ {
					xtype : 'selectfield',
					name : 'AGENCYID',
					label : '单位名称',
					placeHolder : '请选择所属单位',
					valueField: 'AGENCYID',
					displayField:'NAME',
					store : agenymapStore,
					required : true
				} ]
			},
			{
				xtype : 'fieldset',
				title : '基本信息',
				defaults : {
					labelAlign : 'left',
					labelWidth : '40%'
				},
				items : [
		         {
					xtype : 'textfield',
					name : 'COMMITER',
					label : '用户姓名'
		         }, 
		         {
						xtype : 'textfield',
						name : 'DEPARTMENT',
						label : '用户部门'
		         } ]
			} , {
				xtype : 'fieldset',
				title : '详细信息',
				defaults : {
					xtype : 'fieldset'
				},
				items : [
				         {
								xtype : 'textfield',
								name : 'TITLE',
								label : '标题',
								required : true
					         }, 
					         {
									xtype : 'selectfield',
									name : 'NEWSTYPE',
									label : '公告类型',
									placeHolder : '请选择公告类型',
									options : [  {
												text : '通知',
												value : '0'
											}, {
												text : '公告',
												value : '1'
											}
									]
					         },
					         {
									xtype : 'selectfield',
									name : 'URGENTTYPE',
									label : '紧急程度',
									options : [ {
												text : '普通',
												value : '0'
											}, {
												text : '紧急',
												value : '1'
											}
									]
					         }, 
							 {
									xtype : 'textareafield',
									name : 'CONTENTS',
									label : '公告编辑',
									required : true,
									maxLength : 60,
									maxRows : 3
								}
				         ]
			}
				         ],
			listeners : {
				submit : function(form, result) {
					console.log('success', Ext.toArray(arguments));
				},
				exception : function(form, result) {
					console.log('failure', Ext.toArray(arguments));
				}
			},

			dockedItems : [ {
				xtype : 'toolbar',
				dock : 'bottom',
				items : [ {
					xtype : 'spacer'
				}, {
					text : '取消',
					handler : function() {
					document['success'] = 'false';
					}
				}, {
					text : '提交',
					ui : 'confirm',
					handler : function() {
					 var currentNote = form.getRecord(); 
		             form.updateRecord(currentNote); 
	 
	                var errors = currentNote.validate(); 
	                if (!errors.isValid()) { 
	             Ext.Msg.alert('等待...', "带*号选项为必填信息！", Ext.emptyFn); 
	                    return; 
	                } 
	                
					Ext.Msg.confirm("提示", "确认对以上信息进行提交吗？", function(a) {
						if(a+""=="yes"){
						form.submit( {
							waitMsg : {
								message : '正在提交...',
								cls : 'demos-loading'
							},
						 url: "/wipzIphone/news/news_insert.action",           
						 method: 'POST',           
						 success: function(thisForm, action){ 
								Ext.Msg.alert('提示', '提交成功，等待下级审批人‘'+action.NEXTOPERATOR+'’审批！', function() {
									document['success'] = 'true';
								});
								unLoadMask(cmpMask);  
							}
						});
					}else {
						return;
					}
				}
					);
					}
				} ]
			} ]
		};

		if (Ext.is.Phone) {
			formBase.fullscreen = true;
		} else {
			Ext.apply(formBase, {
				autoRender : true,
				floating : true,
				modal : true,
				centered : true,
				hideOnMaskTap : false,
				height : 385,
				width : 480
			});
		}

		form = new Ext.form.FormPanel(formBase);
		form.show();
	}
});